package com.cwt_taxi_xa;

import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.SPUtils;

public class VideoActivity2 extends CWTBaseActivity{

	private SurfaceView mSurfaceView;
	SurfaceHolder sholder;
	Camera camera;
	Camera.Parameters parameters;

	TextView tv_time;
	ImageView iv_retrun,iv_online,iv_center,iv_gps;

	public int chl = 702;


	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1001) {// 刷新时间
				String time = (String) msg.obj;
				tv_time.setText(time);
			}
			else if (msg.what == 1022) {//用于切换tvin通道
//				chl = 702;
				SPUtils.setParam(SPConstants.SPK_backisp, !SPUtils.getParamBoolean(SPConstants.SPK_backisp, SPConstants.SPV_backisp));
				parameters.setPreviewSize(chl, SPUtils.getParamBoolean(SPConstants.SPK_backisp, SPConstants.SPV_backisp)?501:500);
				camera.setParameters(parameters);
			}
			else if (msg.what == 1003) {
				try {
					iv_retrun.callOnClick();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			else if (msg.what == 1007) {
				// 刷新右上角状态位-网络
				iv_online
						.setImageResource((Boolean) msg.obj ? R.drawable.cwtic_bs1_on
								: R.drawable.cwtic_bs1_off);
			} else if (msg.what == 1008) {
				// 刷新右上角状态位-平台
				iv_center
						.setImageResource((Boolean) msg.obj ? R.drawable.cwtic_bs2_on
								: R.drawable.cwtic_bs2_off);
			} else if (msg.what == 1009) {
				// 刷新右上角状态位-定位
				iv_gps
						.setImageResource((Boolean) msg.obj ? R.drawable.cwtic_bs3_on
								: R.drawable.cwtic_bs3_off);
			}

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_video2);

		tv_time = (TextView) findViewById(R.id.textView1);
		iv_retrun = (ImageView) findViewById(R.id.imageView7);
		iv_online = (ImageView) findViewById(R.id.imageView8);
		iv_center = (ImageView) findViewById(R.id.imageView9);
		iv_gps = (ImageView) findViewById(R.id.imageView10);


		ImageView iv_zhishi = (ImageView) findViewById(R.id.imageView_back);
		iv_zhishi.setVisibility(View.GONE);
		iv_zhishi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				handler.sendEmptyMessage(1022);
			}
		});

		mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);

		iv_retrun.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if(camera!=null){
						camera.stopPreview();
						camera.release();
					}
				} catch (Exception e) {
				}
				finish();
			}
		});

		mSurfaceView.getHolder().addCallback(new Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub


				AppLog.e("---------------------surfaceCreated");
				try {
					sholder = holder;

					//友浩系统的奇葩设定。。。
					camera = Camera.open(3);

					parameters = camera.getParameters();

					//获取预览的各种分辨率
//                    List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
//                    for(Size s: supportedPreviewSizes){
//                    	AppLog.e(s.width+"-----------"+s.height);
//                    }
					/**
					 * this.camera = Camera.open(3);
					 this.param = this.camera.getParameters();
					 this.param.setPreviewSize(this.width, this.height);
					 this.param.setPreviewFpsRange(4, 10);
					 this.param.setPictureFormat(256);
					 this.param.set("jpeg-quality", 95);
					 this.param.setPictureSize(1600, 900);
					 this.camera.setParameters(this.param);
					 */

					//友浩系统的奇葩设定。。。
//                    parameters.setPreviewSize(chl, SPUtils.getParamBoolean(SPConstants.SPK_backisp, SPConstants.SPV_backisp)?501:500);//NTSC
//                    parameters.setPreviewSize(chl, 501);//PAL
					parameters.setPreviewSize(chl, 500);//NTSC
//                    parameters.setPreviewSize(720, 576);
//                    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
					camera.setParameters(parameters);
					camera.setPreviewDisplay(sholder);
					camera.startPreview();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				AppLog.e("---------------------surfaceChanged");
				sholder = holder;
//				try {
//					camera.setPreviewDisplay(sholder);
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//                camera.startPreview();
//				try {
//					camera.stopPreview();
//					camera.setPreviewDisplay(holder);
//					camera.startPreview();
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			}
		});

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try {
			camera.stopPreview();
			camera.release();
			camera = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub
		switch (index) {
			case 1:
				handler.sendMessage(handler.obtainMessage(1007, status));
				break;
			case 2:
				handler.sendMessage(handler.obtainMessage(1008, status));
				break;
			case 3:
				handler.sendMessage(handler.obtainMessage(1009, status));
				break;
			default:
				break;
		}
	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		if(type==1002){
			handler.sendMessage(handler.obtainMessage(1001, obj));
		}
		if(type==1014){
			handler.sendEmptyMessage(1003);
		}

	}





}
